<?php
    defined('INDEX') or die ('Acesso direto n&atilde;o permitido');
    
    /**
     * This class manager all request variables.
     * 
     * @author Carlos A. Junior
     */
    class Request
    {
    	const INTEGER 	= 'INTEGER';
    	
    	const STRING	= 'STRING';
    	
    	const DOUBLE	= 'DOUBLE';

    	public function __construct()
        {
            
        }
        
        public function hasPost()
        {
            return sizeof($_POST);
        }
        
        /**
         * Returns the value of an variable of $_GET array.
         * 
         * @param string $key
         * 
         * @param const $type
         * 
         * @return string $value
         */
        public function get ($key, $type)
        {
            return $this->process($key, $type, $_GET);
        }
        
        /**
         * Returns the value of an variable of $_POST array.
         * 
         * @param string $key
         * 
         * @param const $type
         * 
         * @return string $value
         */
        public function post ($key, $type)
        {
            return $this->process($key, $type, $_POST);
        }
        
        /**
         * Returns the sanitized value of an request array ($_POST or $_GET).
         * 
         * @param string $key
         * 
         * @param const $type
         * 
         * @param array $array
         * 
         * @return string $value
         */
        private function process ($key, $type, array $array)
        {
        	if(sizeof($array))
        	{
        		if(array_key_exists($key, $array))
        		{
        			$value = $array[$key];
        			
        			switch ($type)
        			{
        				case self::INTEGER:
        					$value = ApplicationFactory::getParserFactory()->parseInteger($value);
        				break;
        				case self::DOUBLE:
        					$value = ApplicationFactory::getParserFactory()->parseDouble($value);
        				break;
        				case self::STRING:
        					$value = addslashes($value);
        				break;
        			}
        			return $value;
        		}
        		else
        		{
        			return null;
        		}
        	}
        	else
        	{
        		return null;
        	}
        }
    }
?>